﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.DateTimePlugin
{
    public partial class DateTimePage : Mimoza.GUI.WizardPage
    {
        DateTimePluginData m_data;

        public DateTimePage()
        {
            InitializeComponent();
        }

        override public void Init()
        {
            m_data = this.Wizard.UserData as DateTimePluginData;

            checkBoxTimeSync.Checked = m_data.IsTimeSynchronize;

            System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> tzCollection;
            tzCollection = TimeZoneInfo.GetSystemTimeZones();

            foreach (TimeZoneInfo timeZone in tzCollection)
            {
                comboBoxTimeZone.Items.Add(timeZone);
                if ((m_data.TimeZoneStandartName != null && m_data.TimeZoneStandartName == timeZone.StandardName)
                    || (m_data.TimeZoneStandartName == null && timeZone.StandardName == TimeZone.CurrentTimeZone.StandardName))
                {
                    comboBoxTimeZone.SelectedIndex = comboBoxTimeZone.Items.Count - 1;
                }
            }
            
            this.checkBoxTimeSync.CheckedChanged += new System.EventHandler(this.checkBoxTimeSync_CheckedChanged);
            this.comboBoxTimeZone.SelectedIndexChanged += new System.EventHandler(this.comboBoxTimeZone_SelectedIndexChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            m_data.IsTimeSynchronize = checkBoxTimeSync.Checked;

            TimeZoneInfo timeZoneInfo = (comboBoxTimeZone.SelectedItem as TimeZoneInfo);

            m_data.TimeZoneId = timeZoneInfo.Id;
            m_data.TimeZoneStandartName = timeZoneInfo.StandardName;
            m_data.BaseUtcOffsetSec = (int)timeZoneInfo.BaseUtcOffset.TotalSeconds;

            TimeZoneInfo.AdjustmentRule[] rules = timeZoneInfo.GetAdjustmentRules();
            m_data.TimeZoneDelightName = timeZoneInfo.DaylightName;
            m_data.SupportsDaylightSavingTime = timeZoneInfo.SupportsDaylightSavingTime;
            if (rules != null && rules.Length > 0)
                m_data.DaylightDeltaSec = (int)rules[0].DaylightDelta.TotalSeconds;

            m_data.RegistryData = TimeZoneData.Get(m_data.TimeZoneStandartName);
            
            return true;
        }

        private void DateTimePage_Load(object sender, EventArgs e)
        {
  
        }

        private void comboBoxTimeZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void checkBoxTimeSync_CheckedChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }
    }
}
